package com.meitianhui.convenitenbuy.module.order.presenter;

import android.text.TextUtils;

import com.google.gson.Gson;
import com.meitianhui.convenitenbuy.App;
import com.meitianhui.convenitenbuy.base.BaseResponse;
import com.meitianhui.convenitenbuy.config.InfoProvider;
import com.meitianhui.convenitenbuy.http.ResponseObserver;
import com.meitianhui.convenitenbuy.module.order.contract.EdinburghTypeOrderContract;
import com.meitianhui.convenitenbuy.module.order.http.OrderApiDataSource;
import com.meitianhui.convenitenbuy.module.order.model.bean.EmptyInfo;
import com.meitianhui.convenitenbuy.module.order.model.bean.OrderInfo;
import com.meitianhui.convenitenbuy.module.order.ui.fragment.BPPOrderFragment;

import java.util.HashMap;
import java.util.Map;

/**
 * 积分兑 order presenter.
 * Created by Liam on 2018/3/16.
 */

public class BPPOrderPresenter implements EdinburghTypeOrderContract.Presenter {

    private BPPOrderFragment context;
    private EdinburghTypeOrderContract.View view;

    public BPPOrderPresenter(BPPOrderFragment context,
                             EdinburghTypeOrderContract.View view) {
        this.context = context;
        this.view = view;
    }

    @Override
    public void getOrder(String service, String pageIndex, String pageSize,
                         String memberKey, String status) {
        Map<String, String> maps = new HashMap<>();
        maps.put("page_no", pageIndex + "");
        maps.put("page_size", pageSize + "");
        String pageJson = new Gson().toJson(maps);

        Map<String, String> map = new HashMap<>();
        // map.put("member_id", InfoProvider.getUserToken(App.getInstance()).getMember_id());
        // map.put("member_type_key", memberKey);
        map.put("consumer_id", InfoProvider.getUserToken(App.getInstance()).getMember_id());
        // map.put("status", status);
        String paramsJson = new Gson().toJson(map);

        OrderApiDataSource.getInstance()
                .getBPPOrder(InfoProvider.getAppToken(App.getInstance()),
                        service, pageJson, paramsJson)
                .subscribe(new ResponseObserver<BaseResponse<OrderInfo>>(App.getInstance()) {
                    @Override
                    public void onSuccess(BaseResponse<OrderInfo> response) {
                        view.getOrderSuccess(response.data);
                    }

                    @Override
                    public void onFail(int code, String msg) {
                        super.onFail(code, msg);
                        view.getOrderFail(code, msg);
                    }
                });
    }

    @Override
    public void orderOperation(String orderId, String server, String remark,
                               String externalOrderNo, String dataSource) {
        Map<String, String> map = new HashMap<>();
        if (!TextUtils.isEmpty(externalOrderNo)) {
            map.put("external_order_no", externalOrderNo);
        }
        if (!TextUtils.isEmpty(remark)) {
            map.put("remark", remark);
        }
        if (!TextUtils.isEmpty(dataSource)) {
            map.put("data_source", dataSource);
        }
        map.put("order_id", orderId);
        String json = new Gson().toJson(map);
        OrderApiDataSource.getInstance()
                .confirmOrder(InfoProvider.getUserToken(App.getInstance()).getUser_token(), server, json)
                .subscribe(new ResponseObserver<BaseResponse<EmptyInfo>>(App.getInstance()) {
                    @Override
                    public void onSuccess(BaseResponse<EmptyInfo> response) {
                        view.orderOperationSuccess();
                    }

                    @Override
                    public void onFail(int code, String msg) {
                        super.onFail(code, msg);
                        view.orderOperationFail(code, msg);
                    }
                });
    }

    @Override
    public void attachView(Object view) {
    }

    @Override
    public void detachView() {
    }

}
